package lesson3.models;

import components.CircleCollider;
import components.DirectionalTranslator;
import components.HP;
import components.Lifetime;
import java2d.game.GameObject;
import java2d.game.SpriteGameObject;

/**
 * Author:     Zhao Yan
 * DateTime:   2023/4/18 18:45
 */
public class Bullet extends SpriteGameObject implements CircleCollider.CollideEnterEvent {

    public final DirectionalTranslator translator = new DirectionalTranslator();

    public final Lifetime lifetime = new Lifetime();

    public final CircleCollider collider = new CircleCollider();

    public final Spacecraft shooter;

    public final String targetTag;

    public int damage = 10;

    public Bullet(Spacecraft shooter, String targetTag) {
        super("assets/sprites/bullet1.png");

        this.shooter = shooter;
        this.targetTag = targetTag;
        name = "Bullet";
        addComponent(translator);
        translator.speed = 0.5;
        setRenderOrder(-1);

        collider.visible = false;
        collider.collideEnterEvent = this;
        addComponent(collider);
    }

    @Override
    protected void onActivated() {
        lifetime.time = 2;
        addComponent(lifetime);
    }

    @Override
    public void collideEnter(CircleCollider other) {
        GameObject gameObject = other.getGameObject();

        if (gameObject.compareTag(targetTag)) {
//            System.out.println("攻击目标：" + gameObject.name);
            HP hp = gameObject.getComponent(HP.class);

            if (hp != null) {
                hp.setValue(hp.getValue() - damage);
            }

            destroy();
        }
    }
}
